<template>
  <a-modal
    :body-style="bodyStyle"
    :title="title"
    :width="width"
    :visible="visible"
    :confirm-loading="confirmLoading"
    :destroy-on-close="true"
    :ok-button-props="{ props: {disabled: disableSubmit} }"
    cancel-text="关闭"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <uiTemplate
        ref="ui"
        :page-url-data="pageUrlData"
        :select-type="selectType"
        @openTableMenuSelect="openTableMenuSelect"
        @btnClickEvent="btnClickEvent"
        @triggerUiData="triggerUiData"
        @triggerFooterToolbarData="triggerFooterToolbarData"
      />
    </a-spin>
    <uiModalTemplate ref="uiModal" @returnQuickSelectRows="returnQuickSelectRows" />
    <template slot="footer">

      <a-button v-for="(btnItem,index) in footerToolbarList" v-if="btnItem.compTag === 'button' || btnItem.compTag === 'a'" :key="index" :type="btnItem.type" :disabled="btnItem.disabled" @[btnItem.eventName]="btnOnClick(btnItem)">{{ btnItem.name }}</a-button>
      <!--      <a-button @click="handleCancel">取消</a-button>-->
      <!--      <a-button type="primary">驳回</a-button>-->
      <!--      <a-button type="primary" @click="handleOk">确定</a-button>-->
    </template>
  </a-modal>
</template>
<script>
import { changeLoadGeneralModal, changeSelectReturnData, openLoadGeneralModal, changeModelInitWidth, getRefOpenObj } from '@/utils/hyopen'
import { inputChangeEventControlObj, getCallFnByFnType, inputChangeEvent } from '@/utils/hyctr'
import { executeRecord, deleteRecord, batchDeleteRecord } from '@/utils/hyrecord'
import { getGlobalParam } from '../../../../utils/hyrecord'
export default {
  name: 'UiModal',
  components: {
    uiTemplate: () => import('./ui.vue'),
    uiModalTemplate: () => import('./uiModal.vue')
  },
  data() {
    return {
      globalData: {},
      showTableTplKey: 'tableTplList',
      footerToolbarList: [],
      uidata: {}, // 初始化全部UI数据
      triggerOpenParam: {},
      openParam: {},
      formModel: {},
      inputStyle: 'width: 260px',
      screenWidthFix: 1,
      inputWidthFix: 0.75,
      confirmLoading: false,
      bodyStyle: { padding: '0px' },
      dialogVisible: true,
      screenWidth: 0,
      buttonLoading: false,
      buttonDisabled: false,
      width: this.initWidth(),
      title: '操作',
      modalVisible: false,
      loading: false,
      selectType: 'checkbox',
      // selectType: 'radio',
      btnItem: null,
      formItem: null,
      disableSubmit: false,
      selectedRows: null,
      visible: false,
      record: null,
      pageUrlData: { S_PAGE_ID: '', S_PAGE_VER: '' }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    }
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
  },
  methods: {
    triggerFooterToolbarData(footerToolbarList) {
      this.footerToolbarList = footerToolbarList
    },
    triggerUiData(uidata) {
      this.uidata = uidata
    },
    initWidth() {
      const thisObj = this
      // changeModelInitWidth(thisObj)
    },
    showLoadGeneralModal(btnItem, refOpen, triggerOpenParam) {
      const thisObj = this
      if (btnItem.compTag === undefined) {
        if (refOpen.triggerReturn === undefined || refOpen.triggerReturn.length === 0) {
          thisObj.disableSubmit = true
        }
      }
      thisObj.triggerOpenParam = triggerOpenParam
      this.openParam = triggerOpenParam.openParam
      changeLoadGeneralModal(thisObj, btnItem, refOpen, triggerOpenParam)
    },
    openTableMenuSelect(globalData) {
      this.globalData = globalData
    },
    returnQuickSelectRows(rows) {
      console.log('选中##returnQuickSelectRows###数据.', rows)
    },
    btnClickEvent(btnItem, record) {
      if (btnItem.fnType === 98) {
        this.visible = false
      }
    },
    btnOnClick(btnItem) {
      const thisObj = this

      const returnGlobalData = thisObj.globalData[thisObj.showTableTplKey]
      const returnRows = changeSelectReturnData(thisObj, returnGlobalData)
      const tmpGlobalData = {}
      tmpGlobalData.tableId = this.triggerOpenParam.tableId
      tmpGlobalData.initSrcObj = this.triggerOpenParam.initSrcObj
      tmpGlobalData.clickRow = this.triggerOpenParam.openParam
      tmpGlobalData.clickItem = btnItem
      tmpGlobalData.openParam = this.openParam
      tmpGlobalData.formModel = this.formModel
      tmpGlobalData.initData = this.initData
      tmpGlobalData.tableSelectedRow = returnRows
      tmpGlobalData.globalParam = getGlobalParam(this)
      const fn = getCallFnByFnType(btnItem)
      if (fn === 'openGeneralModal') {
        this[fn](btnItem, tmpGlobalData)
      } else {
        this.$emit('returnBtnClickEvent', btnItem, tmpGlobalData)
      }
    },
    openGeneralModal(btnItem, globalData) {
      const refOpenObj = getRefOpenObj(btnItem, globalData)
      if (refOpenObj !== null) {
        btnItem.refOpen = Object.assign({}, refOpenObj)
        openLoadGeneralModal(btnItem, globalData, this)
      } else {
        this.$message.warning('无满足条件开窗数据,请联系管理员.')
      }
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      const thisObj = this
      thisObj.loading = true
      if (thisObj.globalData[thisObj.showTableTplKey] !== undefined) {
        const returnGlobalData = thisObj.globalData[thisObj.showTableTplKey]
        const returnRows = changeSelectReturnData(thisObj, returnGlobalData)
        console.log('returnGlobalData this.formItem', this.formItem, returnRows)
        this.$emit('returnQuickSelectRows', returnRows, this.formItem)
      }
      thisObj.loading = false
      this.visible = false
      thisObj.globalData = {}
      this.$emit('close')
    }
  }
}
</script>
